package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntBaseInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntBaseInfo;
import cc.rengu.igas.cops.common.entity.CopsMchntInfoForBaseAndAcctAndSign;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 商户基本信息表操作实现
 * @Author panky
 * @date 2020/5/18
 **/
public class CopsMchntBaseInfoMapperImpl implements CopsMchntBaseInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<CopsMchntBaseInfo> selectMchntBaseInfoByInstIdAndChainMchntnNo(String instId, String chainMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntBaseInfoByInstIdAndChainMchntnNo");

        CopsMchntBaseInfo copsMchntBaseInfo = new CopsMchntBaseInfo();
        copsMchntBaseInfo.setInstId(instId);
        copsMchntBaseInfo.setChainMchntNo(chainMchntNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, copsMchntBaseInfo, CopsMchntBaseInfo.class);
        return objects.stream().map(item -> (CopsMchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public CopsMchntBaseInfo getMchntBaseInfoByInstIdAndMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsgetMchntBaseInfoByInstIdAndMchntNo");

        CopsMchntBaseInfo copsMchntBaseInfo = new CopsMchntBaseInfo();
        copsMchntBaseInfo.setInstId(instId);
        copsMchntBaseInfo.setMchntNo(mchntNo);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, copsMchntBaseInfo, copsMchntBaseInfo);
        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("查询商户基本信息表不存在，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("查询商户基本信息表失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return copsMchntBaseInfo;
    }

    @Override
    public CopsMchntBaseInfo getMchntBaseInfoByMchntNo(String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsGetMchntBaseInfoByMchntNo");

        CopsMchntBaseInfo opmsMchntBaseInfo = new CopsMchntBaseInfo();
        opmsMchntBaseInfo.setMchntNo(mchntNo);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, opmsMchntBaseInfo, opmsMchntBaseInfo);
        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("查询商户基本信息表无记录，mchntNo:<{}>", mchntNo);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("查询商户基本信息表失败，mchntNo:<{}>", mchntNo);
            return null;
        }

        return opmsMchntBaseInfo;
    }


    /**
     * 多条件查询
     *
     * @param info 传入多个参数
     * @return 商户基本信息
     * @throws Exception 异常
     * @author zhangbin
     */
    @Override
    public PageResult selectCopsMchntBaseInfoByMoreVariable(CopsMchntInfoForBaseAndAcctAndSign info, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntInfoByMoreVariable");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, info, CopsMchntInfoForBaseAndAcctAndSign.class, pageInfo);

        if (null == pageResult) {
            rglog.error("根据多条件查询商户信息失败");
            return null;
        }
        return pageResult;
    }


    /**
     * 删除操作，对基础信息表
     *
     * @param instId  机构号
     * @param mchntNo 商户号
     * @author zhangbin
     */
    @Override
    public int delMchntBaseInfoByInstIdAndMchntNo(String instId, String mchntNo, String oprId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "delMchntBaseInfoByInstIdAndMchntNo");
        CopsMchntBaseInfo copsMchntBaseInfo = new CopsMchntBaseInfo();
        copsMchntBaseInfo.setInstId(instId);
        copsMchntBaseInfo.setMchntNo(mchntNo);
        copsMchntBaseInfo.setLastOprId(oprId);
        copsMchntBaseInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, copsMchntBaseInfo, null);
        if (0 != iReturnCode) {
            rglog.error("删除商户基本信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", copsMchntBaseInfo.getInstId(), copsMchntBaseInfo.getMchntNo(), copsMchntBaseInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    /**
     * 更改商户结算账户信息
     *
     * @param copsMchntBaseInfo
     * @return iReturnCode
     * @throws Exception 异常
     * @author zhangbin
     */
    @Override
    public int updateMchntBaseInfoByInstIdAndMchntNo(CopsMchntBaseInfo copsMchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntBaseInfoByInstIdAndMchntNo");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, copsMchntBaseInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", copsMchntBaseInfo.getInstId(), copsMchntBaseInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    /**
     * 插入商户基础信息
     *
     * @param copsMchntBaseInfo
     * @return iReturnCode
     * @throws Exception 异常
     * @author zhangbin
     */
    @Override
    public int copsInsertMchntBaseInfo(CopsMchntBaseInfo copsMchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsInsertMchntBaseInfo");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, copsMchntBaseInfo, null);
        if (0 != iReturnCode) {
            rglog.error("新增商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", copsMchntBaseInfo.getInstId(), copsMchntBaseInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    /*
     *获取新二级商户号后四位
     */
    public String getNextMchntNoSeq() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getCopsNextMchntNoSeq");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, null);
        if (0 != iReturnCode) {
            rglog.error("获取商户序号SEQUENCE失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        /* 不足长度前补0 */
        String mchntSeqNum = xmlTreeUtil.getXmlTreeStringValue("mchntSeqNum");
        if (mchntSeqNum.length() < 4) {
            String format = "%0" + (4 - mchntSeqNum.length()) + "d";
            return String.format(format, 0) + mchntSeqNum;
        } else {
            return mchntSeqNum;
        }
    }

    /**
     * 修改基本信息 状态
     *
     * @param copsMchntBaseInfo
     * @return
     * @zhangbin
     */
    @Override
    public int updateMchntBaseInfoByIdToAuditStatus(CopsMchntBaseInfo copsMchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntBaseInfoAuditStatus");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, copsMchntBaseInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户基本信息的状态失败,Id:<{}>,iReturnCode:<{}>", copsMchntBaseInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }
}
